﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace UcAsp.Net.Extract.Plug.Rayli.BLL
{
    public class FilesConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 获取网站设置
        /// </summary>
        public static Model.FilesConfig GetFilesConfig(string filename)
        {

                Model.FilesConfig model = new Model.FilesConfig();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\config\\Rayli\\" + filename + ".xml");
                string xmlpath = ("root");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {
                        foreach (XmlNode xn in xns)
                        {

                            for (int i = 0; i < xn.ChildNodes.Count; i++)
                            {
                                XmlToObject(model, xn.Name, xn.InnerText);
                            }

                        }
                    }
                }

                return model;

        }
        /// <summary>
        /// 保存网站设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SaveFilesConfig(Model.FilesConfig model,string filename)
        {
            if (!File.Exists(path + "\\config\\Rayli\\" + filename + ".xml"))
            {
                byte[] content = System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><FileName /><Name /><Url /><URLReg /><TitleReg /><ContentReg /><ImageReg /><PageReg /><PageNumber /><RootPath /><PageUrl /><ClearReg /><CategorieID /><PicTitleReg /><PicContentReg /><PicImageReg /><PicPage /></root>");
                FileStream file = File.Create(path + "\\config\\Rayli\\" + filename + ".xml");
                Model.Config config = new Model.Config { DefinedName = filename, DefinedTitle = model.Name };
                BLL.Config.InsertConfig(config);
                file.Write(content, 0, content.Length);
                file.Dispose();
            }
            else
            {
                Model.Config config = new Model.Config { DefinedName = filename, DefinedTitle = model.Name };
                BLL.Config.UpdateConfig(config); 
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\Rayli\\" + filename + ".xml");
            string xmlpath = "root";
            XmlNode root = xmlDoc.SelectSingleNode(xmlpath);
            Type type = model.GetType();
            System.Reflection.PropertyInfo[] propertys = type.GetProperties();
            foreach (System.Reflection.PropertyInfo t in propertys)
            {
                XmlElement node = (XmlElement)root.SelectSingleNode(t.Name);
                System.Reflection.PropertyInfo property = type.GetProperty(t.Name);
                //当model值为空是跳过
                try
                {
                    node.InnerText = property.GetValue(model, null).ToString();
                }
                catch { }
            }
            xmlDoc.Save(path + "\\config\\Rayli\\" + filename + ".xml");
            return true;
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
    }
}
